﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace z_tool.config
{
    /// <summary>
    /// 配置项
    /// </summary>
    public static class baseconfig
    {
        public static Action<string> log;
        public static Action listpath;
        public static Action save_temporary;
        public static Action db_reset;
        private static string _root_path;
        private static string _db_conn_str;
        private static List<table_info> _tables = new List<table_info>();
        private static List<itemx> _generate_type = new List<itemx>();
        private static List<folder_item> _folder_items = new List<folder_item>();
        /// <summary>
        /// 初始化
        /// </summary>
        public static bool initial { get; set; }
        /// <summary>
        /// 触发数据变动
        /// </summary>
        /// <param name="type">选择目录=1 连接数据库=2 选择表=3 设置列=4 设置生成方式=5 配置生成项=6</param>
        public static void change(int type = 0)
        {
            if (baseconfig.initial) return;
            save_temporary();
            if (type == 2) db_reset(); //重置数据库连接
            if (type >= 3) listpath();
            //保存table、generate_type到本地文件 
        }
        /// <summary>
        /// 项目目录
        /// </summary>
        public static string root_path
        {
            get { return _root_path; }
            set
            {
                _root_path = value; change(1);
            }
        }
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string db_conn_str
        {
            get { return _db_conn_str; }
            set { _db_conn_str = value; change(2); }
        }

        /// <summary>
        /// 数据库连接字符串map
        /// </summary>
        public static Dictionary<string, string> db_conn_str_dic
        {
            get
            {
                var dic = new Dictionary<string, string>();
                foreach (var str in _db_conn_str.Split(';'))
                {
                    if (string.IsNullOrEmpty(str)) continue;
                    var str0 = str.Split('=');
                    dic.Add(str0[0], str0[1]);
                }
                return dic;
            }
        }
        /// <summary>
        /// 数据库名称
        /// </summary>
        public static string table_schema
        {
            get
            {
                DBHelper db = new DBHelper(_db_conn_str);
                string outmsg = "";
                if (!db.checkopen(out outmsg))
                {
                    log(outmsg);
                    return "";
                }
                string dbname = db.GetDataSet("SELECT DATABASE() dbname").Rows[0][0].ToString();
                db.CloseConnection();
                return dbname;
            }
        }
        /// <summary>
        /// 当前数据库的表
        /// </summary>
        public static List<table_info> tables
        {
            set
            {
                _tables = value;
            }
            get
            {
                if (string.IsNullOrEmpty(_db_conn_str)) return _tables;
                table_info[] _tables_copy = new table_info[_tables.Count()];
                _tables.CopyTo(_tables_copy);
                List<table_info> _tables_list = _tables_copy.ToList();
                _tables.Clear();
                DBHelper db = new DBHelper(_db_conn_str);
                DataTable data = db.GetDataSet("SELECT t.TABLE_NAME,t.TABLE_COMMENT FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = DATABASE()");
                itemx six = baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault();
                int substringlen = 0;
                if (six != null)
                {
                    substringlen = six.setting["tablePrefix"].ToString().Length;
                }
                int index = 0;
                foreach (DataRow dr in data.Rows)
                {
                    index++;
                    var ckinfo = _tables_list.Where(s => s.table_name == dr["TABLE_NAME"].ToString().Substring(substringlen)).FirstOrDefault();
                    if (ckinfo != null && ckinfo.db_conn_str == _db_conn_str)
                    {
                        ckinfo.log = log;
                        ckinfo.db_conn_str = _db_conn_str;
                        ckinfo.table_name = dr["TABLE_NAME"].ToString().Substring(substringlen);
                        ckinfo.table_name_original = dr["TABLE_NAME"].ToString();
                        ckinfo.table_comment = dr["TABLE_COMMENT"].ToString();
                        ckinfo.index = index;
                        _tables.Add(ckinfo);
                        continue;
                    }
                    _tables.Add(new table_info()
                    {
                        log = log,
                        db_conn_str = _db_conn_str,
                        table_name = dr["TABLE_NAME"].ToString().Substring(substringlen),
                        table_name_original = dr["TABLE_NAME"].ToString(),
                        table_comment = dr["TABLE_COMMENT"].ToString(),
                        index = index
                    }); ;
                }
                db.CloseConnection();
                return _tables.OrderBy(s => s.hasmenu == true ? 0 : 1).ToList();
            }
        }
        /// <summary>
        /// 生成选项
        /// </summary>
        public static List<itemx> generate_type
        {
            set
            {
                _generate_type = value;
            }
            get
            {
                if (_generate_type.Count() != 0) return _generate_type;
                var _out = new List<itemx>();
                var itemx1 = new itemx() { name = "1:生成java_1.0_all", value = "1", description = "控制器、类、业务层", selected = true };
                itemx1.setting = new Dictionary<string, object>();
                itemx1.setting.Add("package", "cn.krui.project");
                itemx1.setting.Add("module", "article");
                itemx1.setting.Add("version_key", "version_id");
                itemx1.setting.Add("tablePrefix", "qr_");
                itemx1.setting.Add("description", "xx管理模块");
                itemx1.setting.Add("parent_key", "pid,parent_id");
                itemx1.setting.Add("parent_name", "name,title");
                itemx1.setting.Add("dynamic_datasource", "master");
                _out.Add(itemx1);
                var itemx2 = new itemx() { name = "2:生成vue_sys_1.0_all", value = "2", description = "管理端页面", selected = false };
                itemx2.setting = new Dictionary<string, object>();
                itemx2.setting.Add("module", "article");
                itemx2.setting.Add("tablePrefix", "qr_");
                itemx2.setting.Add("parent_key", "pid,parent_id");
                itemx2.setting.Add("parent_name", "name,title");
                _out.Add(itemx2);
                _generate_type.AddRange(_out);
                return _out;
            }
        }
        /// <summary>
        /// 生成文件信息
        /// </summary>
        public static List<folder_item> folder_Items
        {
            get
            {
                _folder_items.Clear();
                var _ix = _generate_type.Where(s => s.selected == true).FirstOrDefault();
                if (_ix.value == "1")
                {
                    #region java_1.0 
                    string _folder_bean = string.Format("/bean/{0}", _ix.setting["module"].ToString());
                    string _folder_mybatisplusmapper = string.Format("/mapper/{0}", _ix.setting["module"].ToString());
                    string _folder_controller = string.Format("/controller/{0}", _ix.setting["module"].ToString());
                    string _folder_controller_base = string.Format("/controller/{0}", _ix.setting["module"].ToString());
                    string _folder_service = string.Format("/service/{0}", _ix.setting["module"].ToString());
                    string _folder_pub_sub = string.Format("/pub_sub/{0}", _ix.setting["module"].ToString());
                    var tblist = _tables.Where(s => s.selected == true).ToList();
                    #region 生成文件
                    //生成外围代码 
                    //application
                    _folder_items.Add(new folder_item()
                    {
                        folder = "",
                        path = "",
                        filepath = "Application.java"
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = "",
                        path = "",
                        filepath = "Swagger2Init.java"
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = "",
                        path = "",
                        filepath = "tokenFilterSetting.java"
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = "",
                        path = "",
                        filepath = "PubSubConfig.java"
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_pub_sub,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/Subscriber_{1}.java", _folder_controller, _ix.setting["module"].ToString())
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_pub_sub,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/SubThread_{1}.java", _folder_controller, _ix.setting["module"].ToString())
                    });
                    //pom.xml
                    _folder_items.Add(new folder_item()
                    {
                        folder = "",
                        path = "",
                        filepath = "pom.xml"
                    });
                    //resources_application.properties
                    _folder_items.Add(new folder_item()
                    {
                        folder = "/resources",
                        path = "/resources",
                        filepath = "/resources/application.properties"
                    });
                    //resources_crebas.sql
                    _folder_items.Add(new folder_item()
                    {
                        folder = "/resources",
                        path = "/resources",
                        filepath = "/resources/crebas.sql"
                    });
                    //resources_logback
                    _folder_items.Add(new folder_item()
                    {
                        folder = "/resources",
                        path = "/resources",
                        filepath = "/resources/logback-spring.xml"
                    });
                    //controller_base
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_controller_base,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/{1}Base.java", _folder_controller, _ix.setting["module"].ToString())
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_controller_base,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/{1}BaseController.java", _folder_controller, _ix.setting["module"].ToString())
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_controller_base,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/actuatorController.java", _folder_controller)
                    });
                    _folder_items.Add(new folder_item()
                    {
                        folder = _folder_controller_base,
                        path = string.Format("{0}", _folder_controller),
                        filepath = string.Format("{0}/{1}CheckController.java", _folder_controller, _ix.setting["module"].ToString())
                    });
                    foreach (table_info ti in tblist)
                    {
                        //生成bean
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_bean,
                            path = string.Format("{0}/{1}", _folder_bean, ti.table_name),
                            filepath = string.Format("{0}/{1}/{1}.java", _folder_bean, ti.table_name)
                        });
                        //生成mybatisplus mapper
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_mybatisplusmapper,
                            path = string.Format("{0}/{1}", _folder_mybatisplusmapper, ti.table_name),
                            filepath = string.Format("{0}/{1}/{1}Mapper.java", _folder_mybatisplusmapper, ti.table_name)
                        });
                        //生成controller
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_controller,
                            path = string.Format("{0}/{1}", _folder_controller, ti.table_name),
                            filepath = string.Format("{0}/{1}/{1}Controller.java", _folder_controller, ti.table_name)
                        });
                        //生成service
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_service,
                            path = string.Format("{0}/{1}", _folder_service, ti.table_name),
                            filepath = string.Format("{0}/{1}/{1}Service.java", _folder_service, ti.table_name)
                        });

                    }
                    #endregion
                    #endregion
                }
                else if (_ix.value == "2")
                {
                    string _folder_api = string.Format("/src/api/project/{0}", _ix.setting["module"].ToString());
                    string _folder_views = string.Format("/src/views/project/{0}", _ix.setting["module"].ToString());
                    var tblist = _tables.Where(s => s.selected == true).ToList();
                    #region 生成文件
                    foreach (table_info ti in tblist)
                    {
                        //api
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_api,
                            path = string.Format("{0}", _folder_api),
                            filepath = string.Format("{0}/project_{1}_{2}.js", _folder_api, _ix.setting["module"].ToString(), ti.table_name)
                        });
                        //view
                        _folder_items.Add(new folder_item()
                        {
                            folder = _folder_views,
                            path = string.Format("{0}/{1}", _folder_views, ti.table_name),
                            filepath = string.Format("{0}/{1}/index.vue", _folder_views, ti.table_name)
                        });
                        if (ti.editwindow == true)
                        {
                            _folder_items.Add(new folder_item()
                            {
                                folder = _folder_views,
                                path = string.Format("{0}/{1}", _folder_views, ti.table_name),
                                filepath = string.Format("{0}/{1}/edit.vue", _folder_views, ti.table_name)
                            });
                        }
                        if (ti.hasedit)
                        {
                            _folder_items.Add(new folder_item()
                            {
                                folder = _folder_views,
                                path = string.Format("{0}/{1}", _folder_views, ti.table_name),
                                filepath = string.Format("{0}/{1}/info.vue", _folder_views, ti.table_name)
                            });
                        }
                    }
                    #endregion
                }
                return _folder_items;
            }
        }
    }
    /// <summary>
    /// 表信息
    /// </summary>
    public class table_info
    {
        [JsonIgnore]
        public Action<string> log { get; set; }
        private string _db_conn_str;
        private string _table_name;
        private string _table_name_original;
        private string _table_comment;
        private bool _selected = true;
        private List<string> _java_import = new List<string>();
        private bool _hasmenu = true;
        private string _parent_info = "";
        private bool _istree = false;
        private int _index;
        private bool _hasedit = true;
        private bool _haseditor = false;
        private bool _hasimg = false;
        private bool _hasfile = false;
        private bool _editwindow = false;
        private bool _editright = false;
        private List<column_info> _colums = new List<column_info>();
        private string _name_str = "";
        private string _pk_str = "";
        private string _pk_str_up = "";
        private string _version_str = "";
        private Dictionary<string,string> _ignore_str = new Dictionary<string, string>();
        private List<string> _union_tables = new List<string>();
        private bool _auto_increment;
        private bool _has_api = false;
        private string _treepk = "";
        private string _treepkup = "";
        /// <summary>
        /// 树形父级key
        /// </summary>
        public string treepk
        {
            get { return _treepk; }
            set { _treepk = value; }
        }
        /// <summary>
        /// 树形父级key
        /// </summary>
        public string treepkup
        {
            get { return _treepkup; }
            set { _treepkup = value; }
        }
        /// <summary>
        /// 是否有API服务
        /// </summary>
        public bool has_api
        {
            get { return _has_api; }
            set { _has_api = value; }
        }
        /// <summary>
        /// 主键是否自增
        /// </summary>
        public bool auto_increment
        {
            get { return _auto_increment; }
            set { _auto_increment = value; }
        }
        /// <summary>
        /// 是否有图片上传
        /// </summary>
        public bool hasimg
        {
            get { return _hasimg; }
            set { _hasimg = value; }
        }
        /// <summary>
        /// 是否有文件上传
        /// </summary>
        public bool hasfile
        {
            get { return _hasfile; }
            set { _hasfile = value; }
        }
        /// <summary>
        /// 是否有编辑框
        /// </summary>
        public bool haseditor
        {
            get { return _haseditor; }
            set { _haseditor = value; }
        }
        /// <summary>
        /// 关联表
        /// </summary>
        public List<string> union_tables
        {
            get { return _union_tables; }
            set { _union_tables = value; }
        }
        /// <summary>
        /// 忽略的字段
        /// </summary>
        public Dictionary<string, string> ignore_str
        {
            get { return _ignore_str; }
        }
        /// <summary>
        /// 主键字段
        /// </summary>
        public string pk_str
        {
            get { return _pk_str; }
            set { _pk_str = value; }
        }
        public string pk_str_up
        {
            get { return _pk_str_up; }
            set { _pk_str_up = value; }
        }

        /// <summary>
        /// 版本字段
        /// </summary>
        public string version_str
        {
            get { return _version_str; }
            set { _version_str = value; }
        }
        /// <summary>
        /// 内容字段
        /// </summary>
        public string name_str
        {
            get { return _name_str; }
            set { _name_str = value; }
        }
        /// <summary>
        /// 是否检索
        /// </summary>
        [JsonIgnore]
        public bool search
        {
            get
            {
                return colums.Count(s => s.search == true) > 0;
            }
        }
        /// <summary>
        /// 数据库连接
        /// </summary>
        public string db_conn_str
        {
            get { return _db_conn_str; }
            set { _db_conn_str = value; }
        }
        /// <summary>
        /// 表名称
        /// </summary>
        public string table_name
        {
            get { return _table_name; }
            set { _table_name = value; }
        }
        /// <summary>
        /// 表名称_驼峰
        /// </summary>
        public string table_name_hump
        {
            get
            {
                var array = this._table_name.Split('_');
                var list = new List<string>();
                foreach(string item in array)
                {
                    string first = item[0].ToString().ToUpper();
                    if (item.Length > 1)
                    {
                        first = first + item.Substring(1);
                    }
                    list.Add(first);
                }
                return String.Join("", list);
            }
        }

        /// <summary>
        /// 表名称_驼峰_首字母消息
        /// </summary>
        public string table_name_hump_1
        {
            get
            {
                var array = this._table_name.Split('_');
                var list = new List<string>();
                int i = 0;
                foreach (string item in array)
                {
                    string first = item[0].ToString().ToUpper();
                    if (item.Length > 1)
                    {
                        first = first + item.Substring(1);
                    }
                    if (i == 0)
                    {
                        first = item[0].ToString().ToLower();
                        first = first + item.Substring(1);
                    }
                    list.Add(first);
                    i++;
                }
                return String.Join("", list);
            }
        }
        /// <summary>
        /// 表名称没有截取
        /// </summary>
        public string table_name_original
        {
            get { return _table_name_original; }
            set { _table_name_original = value; }
        }
        /// <summary>
        /// 表描述
        /// </summary>
        public string table_comment
        {
            get { return _table_comment; }
            set { _table_comment = value; }
        }
        /// <summary>
        /// 是否选择
        /// </summary>
        public bool selected
        {
            get { return _selected; }
            set { _selected = value; }
        }
        /// <summary>
        /// 列信息
        /// </summary>
        public List<column_info> colums
        {
            set
            {
                _colums = value;
            }
            get
            {
                if (baseconfig.initial) return _colums;
                column_info[] _column_copy = new column_info[_colums.Count()];
                _colums.CopyTo(_column_copy);
                List<column_info> _columns_list = _column_copy.ToList();
                _colums.Clear();
                DBHelper db = new DBHelper(_db_conn_str);
                DataTable data = db.GetDataSet(string.Format("SELECT c.COLUMN_NAME,c.ORDINAL_POSITION,IFNULL(c.COLUMN_DEFAULT,'nul') COLUMN_DEFAULT,c.IS_NULLABLE,c.DATA_TYPE,c.COLUMN_COMMENT,c.COLUMN_TYPE,c.COLUMN_KEY,c.EXTRA FROM information_schema.COLUMNS c WHERE c.TABLE_SCHEMA = DATABASE() AND c.TABLE_NAME = '{0}'", _table_name_original));
                itemx six = baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault();
                foreach (DataRow dr in data.Rows)
                {
                    var ckinfo = _columns_list.Where(s => s.column_name == dr["COLUMN_NAME"].ToString()).FirstOrDefault();
                    var _column_info = new column_info();
                    if (ckinfo != null)
                    {
                        _column_info = ckinfo;
                        _column_info.column_name = dr["COLUMN_NAME"].ToString();
                        _column_info.ordinal_position = int.Parse(dr["ORDINAL_POSITION"].ToString());
                        _column_info.column_default = dr["COLUMN_DEFAULT"].ToString();
                        _column_info.is_nullable_data = dr["IS_NULLABLE"].ToString();
                        _column_info.data_type = dr["DATA_TYPE"].ToString();
                        _column_info.column_type = dr["COLUMN_TYPE"].ToString().Replace(" unsigned", "");
                        _column_info.column_comment_full = dr["COLUMN_COMMENT"].ToString().Replace(Environment.NewLine, "");
                        _column_info.prikey_set = dr["COLUMN_KEY"].ToString();
                        _column_info.auto_increment_set = dr["EXTRA"].ToString();
                    }
                    else
                    {
                        _column_info = new column_info()
                        {
                            column_name = dr["COLUMN_NAME"].ToString(),
                            ordinal_position = int.Parse(dr["ORDINAL_POSITION"].ToString()),
                            column_default = dr["COLUMN_DEFAULT"].ToString(),
                            is_nullable_data = dr["IS_NULLABLE"].ToString(),
                            data_type = dr["DATA_TYPE"].ToString(),
                            column_type = dr["COLUMN_TYPE"].ToString().Replace(" unsigned", ""),
                            column_comment_full = dr["COLUMN_COMMENT"].ToString().Replace(Environment.NewLine, ""),
                            prikey_set = dr["COLUMN_KEY"].ToString(),
                            auto_increment_set = dr["EXTRA"].ToString()
                        };
                    }
                    if(_column_info.auto_increment_get) this._auto_increment = true;
                    if (_column_info.prikey_get) _pk_str = _column_info.column_name;
                    if (_column_info.prikey_get) _pk_str_up = _column_info.column_name[0].ToString().ToUpper() + _column_info.column_name.Substring(1);
                    if (six != null && six.value == "1") //用于java生成
                    {
                        if (dr["COLUMN_NAME"].ToString().ToLower() == six.setting["version_key"].ToString().ToLower())
                        {
                            _column_info.version_key = true;
                            _java_import.Add("import com.baomidou.mybatisplus.annotation.Version;");
                            _version_str = _column_info.column_name;
                        }
                    }
                    if (six != null)
                    {
                        if (six.setting.ContainsKey("parent_name") && six.setting["parent_name"].ToString().Contains(_column_info.column_name.ToLower())) _name_str = _column_info.column_name;
                        if (six.setting.ContainsKey("parent_key") && six.setting["parent_key"].ToString().Contains(_column_info.column_name.ToLower()))
                        {
                            _treepk = _column_info.column_name;
                            _treepkup = _column_info.column_name[0].ToString().ToUpper() + _column_info.column_name.Substring(1);
                        }
                    }
                    if (_column_info.ignore)
                    { 
                        if (!_ignore_str.ContainsKey(_column_info.column_name))
                            _ignore_str.Add(_column_info.column_name,"");
                    }
                    if (_istree)
                    {
                        _java_import.Add("import java.util.List;");
                    }
                    _colums.Add(_column_info);
                    if (_column_info.data_type_java == "BigInteger")
                    {
                        _java_import.Add("import java.math.BigInteger;");
                    }
                    else if (_column_info.data_type_java == "BigDecimal")
                    {
                        _java_import.Add("import java.math.BigDecimal;");
                    }
                    else if (_column_info.data_type_java == "Date")
                    {
                        _java_import.Add("import java.sql.Date;");
                    }
                    else if (_column_info.data_type_java == "Time")
                    {
                        _java_import.Add("import java.sql.Time;");
                    }
                    else if (_column_info.data_type_java == "Timestamp")
                    {
                        _java_import.Add("import java.sql.Timestamp;");
                    }
                    _java_import = _java_import.Distinct().ToList();
                }
                db.CloseConnection();
                return _colums;
            }
        }
        /// <summary>
        /// java类型引用
        /// </summary>
        public List<string> java_import
        {
            get { return _java_import; }
        }
        /// <summary>
        /// 是否有菜单
        /// </summary>
        public bool hasmenu
        {
            get { return _hasmenu; }
            set { _hasmenu = value; }
        }
        /// <summary>
        /// 父级表信息
        /// </summary>
        public string parent_info
        {
            get { return _parent_info; }
            set { _parent_info = value; }
        }
        /// <summary>
        /// 父级信息
        /// </summary>
        public table_info p_tab_info
        {
            get
            {
                if (string.IsNullOrEmpty(_parent_info)) return null;
                var tinfo = baseconfig.tables.Where(s => s.table_name == _parent_info).FirstOrDefault();
                return tinfo;
            }
        }
        /// <summary>
        /// 是否树形
        /// </summary>
        public bool istree
        {
            get { return _istree; }
            set { _istree = value; }
        }
        /// <summary>
        /// 顺序
        /// </summary>
        public int index
        {
            get { return _index; }
            set { _index = value; }
        }
        /// <summary>
        /// 新窗口编辑
        /// </summary>
        public bool editwindow
        {
            get { return _editwindow; }
            set { _editwindow = value; }
        }
        /// <summary>
        /// 右侧编辑
        /// </summary>
        public bool editright
        {
            get { return _editright; }
            set { _editright = value; }
        }
        /// <summary>
        /// 是否编辑
        /// </summary>
        public bool hasedit
        {
            get { return _hasedit; }
            set { _hasedit = value; }
        }
    }
    /// <summary>
    /// 列信息
    /// </summary>
    public class column_info
    {
        private bool _selected = true;
        private bool _list_show = true;
        private string _column_name;
        private int _ordinal_position;
        private string _column_default;
        private string _is_nullable;
        private string _data_type;
        private string _column_type;
        private string _column_comment_full;
        private string _prikey;
        private string _auto_increment;
        private bool _version_key = false;
        private string _form_type = "input";
        private bool _search = false;
        private Int64 _min = 0;
        private Int64 _max = 10;
        private int _precision = 0;
        private string _default = "";
        private string _parent_info = "";


        /// <summary>
        /// 父级表信息
        /// </summary>
        public string parent_info
        {
            get { return _parent_info; }
            set { _parent_info = value; }
        }
        /// <summary>
        /// 默认值
        /// </summary>
        public string defaultvalue
        {
            get { return _default; }
        }
        /// <summary>
        /// 最小值
        /// </summary>
        public Int64 min
        {
            get { return _min; }
        }
        /// <summary>
        /// 最大值
        /// </summary>
        public Int64 max
        {
            get { return _max; }
        }
        /// <summary>
        /// 精度
        /// </summary>
        public int precision
        {
            get { return _precision; }
        }
        /// <summary>
        /// 服务端排除字段
        /// </summary>
        public bool ignore
        {
            get
            {
                if (column_name.ToLower() == "group_id") return true;
                return false;
            }
        }
        /// <summary>
        /// 是否检索字段
        /// </summary>
        public bool search
        {
            get { return _search; }
            set { _search = value; }
        }
        /// <summary>
        /// 表单类型
        /// </summary>
        public string form_type
        {
            get { return _form_type; }
            set { _form_type = value; }
        }
        /// <summary>
        /// 列名称
        /// </summary>
        public string column_name
        {
            get { return _column_name; }
            set { _column_name = value; }
        }
        /// <summary>
        /// 列名称首字母大写
        /// </summary>
        public string column_name_upp
        {
            get { return _column_name[0].ToString().ToUpper() + _column_name.Substring(1); }
        }
        /// <summary>
        /// 列排序
        /// </summary>
        public int ordinal_position
        {
            get { return _ordinal_position; }
            set { _ordinal_position = value; }
        }
        /// <summary>
        /// 列默认值
        /// </summary>
        public string column_default
        {
            get { return _column_default; }
            set { _column_default = value; }
        }
        /// <summary>
        /// 列是否允许为空
        /// </summary>
        public bool is_nullable
        {
            get { return _is_nullable == "NO" ? false : true; }
        }
        public string is_nullable_data
        {
            set { _is_nullable = value; }
        }
        /// <summary>
        /// 数据类型
        /// </summary>
        public string data_type
        {
            get { return _data_type; }
            set { _data_type = value; }
        }
        /// <summary>
        /// 列类型
        /// </summary>
        public string column_type
        {
            get { return _column_type; }
            set { _column_type = value; }
        }
        /// <summary>
        /// java数据类型
        /// </summary>
        public string data_type_java
        {
            get
            {
                var __data_type = _data_type.ToUpper();
                StringBuilder sb = new StringBuilder();
                string[] percision;
                switch (__data_type)
                {
                    case "VARCHAR":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        _default = "''";
                        return "String";
                    case "CHAR":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        _default = "''";
                        return "String";
                    case "BLOB":
                        _min = 0;
                        _max = 2000;
                        _default = "''";
                        return "byte[]";
                    case "TINYTEXT":
                        _min = 0;
                        _max = 10;
                        _default = "''";
                        return "String";
                    case "TEXT":
                        _min = 0;
                        _max = 2000;
                        _default = "''";
                        return "String";
                    case "MEDIUMTEXT":
                        _min = 0;
                        _max = 3000;
                        return "String";
                    case "LONGTEXT":
                        _min = 0;
                        _max = 5000;
                        _default = "''";
                        return "String";
                    case "INTEGER":
                        _min = 0;
                        _max = 99999999;
                        _default = "0";
                        return "Long";
                    case "TINYINT":
                        //if (_column_type.ToUpper() == "TINYINT(1)")
                        //{
                        //    _default = 0;
                        //    return "Boolean ";
                        //}
                        //else
                        //{
                            _min = 0;
                            _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                            sb.Clear();
                            for (int i = 0; i < _max; i++) { sb.Append("9"); }
                            _max = Int64.Parse(sb.ToString());
                            _default = "0";
                            return "Long";
                        //}
                    case "INT":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        _default = "0";
                        return "Integer";
                    case "SMALLINT":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        _default = "0";
                        return "Integer";
                    case "MEDIUMINT":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        _default = "0";
                        return "Integer";
                    case "BIT":
                        return "Boolean";
                    case "BIGINT":
                        _min = 0;
                        _max = int.Parse(_column_type.Replace(_data_type, "").Replace("(", "").Replace(")", ""));
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        if (sb.Length > 19)
                        {
                            _max = Int64.Parse("9223372036854775807");
                        }
                        else
                        {
                            _max = Int64.Parse(sb.ToString());
                        }
                        _default = "0";
                        return "BigInteger";
                    case "FLOAT":
                        percision = _column_type.Replace(_data_type, "").Replace("(", "").Replace(")", "").Split(',');
                        _min = 0;
                        _max = int.Parse(percision[0]);
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        int.TryParse(percision[1], out _precision);
                        _default = "0.0";
                        return "Float";
                    case "DOUBLE":
                        percision = _column_type.Replace(_data_type, "").Replace("(", "").Replace(")", "").Split(',');
                        _min = 0;
                        _max = int.Parse(percision[0]);
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        int.TryParse(percision[1], out _precision);
                        _default = "0.0";
                        return "Double";
                    case "DECIMAL":
                        percision = _column_type.Replace(_data_type, "").Replace("(", "").Replace(")", "").Split(',');
                        _min = 0;
                        _max = int.Parse(percision[0]);
                        sb.Clear();
                        for (int i = 0; i < _max; i++) { sb.Append("9"); }
                        _max = Int64.Parse(sb.ToString());
                        int.TryParse(percision[1], out _precision);
                        _default = "0.0";
                        return "BigDecimal";
                    case "BOOLEAN":
                        _default = "false";
                        return "Boolean";
                    case "ID":
                        _min = 0;
                        _max = 99999999;
                        _default = "0";
                        return "Long";
                    case "DATE":
                        _default = string.Format("'{0}'", DateTime.Now.ToString());
                        return "Date";
                    case "TIME":
                        _default = string.Format("'{0}'", DateTime.Now.ToString());
                        return "Time";
                    case "DATETIME":
                        _default = string.Format("'{0}'", DateTime.Now.ToString());
                        return "Timestamp";
                    case "TIMESTAMP":
                        _default = string.Format("'{0}'", DateTime.Now.ToString());
                        return "Timestamp";
                    case "YEAR":
                        _default = string.Format("'{0}'", DateTime.Now.ToString());
                        return "Date";
                }
                return "";
            }
        }
        /// <summary>
        /// 列描述
        /// </summary>
        public string column_comment
        {
            get { return _column_comment_full.Split(':')[0]; }
        }
        /// <summary>
        /// 完成描述
        /// </summary>
        public string column_comment_full
        {
            get { return _column_comment_full; }
            set { _column_comment_full = value; }
        }
        /// <summary>
        /// 是否选择/编辑
        /// </summary>
        public bool selected
        {
            get { return _selected; }
            set { _selected = value; }
        }
        /// <summary>
        /// 是否展示
        /// </summary>
        public bool list_show
        {
            get { return _list_show; }
            set { _list_show = value; }
        }
        /// <summary>
        /// 主键
        /// </summary>
        public bool prikey_get
        {
            get
            {
                if (string.IsNullOrEmpty(_prikey)) return false;
                return _prikey.ToUpper() == "PRI";
            }
        }
        public string prikey_set
        {
            set { _prikey = value; }
        }
        /// <summary>
        /// 是否自增
        /// </summary>
        public bool auto_increment_get
        {
            get { return _auto_increment == "auto_increment"; }
        }
        public string auto_increment_set
        {
            set { _auto_increment = value; }
        }
        /// <summary>
        /// 是否版本字段（乐观锁）
        /// </summary>
        public bool version_key
        {
            get { return _version_key; }
            set { _version_key = value; }
        }
    }
    /// <summary>
    /// 生成配置项
    /// </summary>
    public class itemx
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string value { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string description { get; set; }
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool selected { get; set; }
        /// <summary>
        /// 配置信息
        /// </summary>
        public Dictionary<string, object> setting { get; set; }

    }
    /// <summary>
    /// 文件夹信息
    /// </summary>
    public class folder_item
    {
        /// <summary>
        /// 文件夹前段名称
        /// </summary>
        public string folder { get; set; }
        /// <summary>
        /// 文件夹路径
        /// </summary>
        public string path { get; set; }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string filepath { get; set; }
    }

    /// <summary>
    /// itemxx
    /// </summary>
    public class itemxx
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string value { get; set; }
    }
}
